<template>
<div >
    <el-dialog title="Select Branch" :visible.sync="branchTableVisible" :before-close="handleClose" v-if="branchTableVisible">
        <el-tree :data="subBranchList" show-checkbox :check-strictly="true" @check-change="checkChange" default-expand-all node-key="brhId" ref="tree" highlight-current :props="defaultProps">
            <!--
            <span slot-scope="{ node, data }">
                <span>{{ node.label }}</span>
                <span>{{node.brhId}}</span>
            </span>
            -->
        </el-tree>
        <div class="btnArea mt20 tac">
            <el-button @click="branchTableVisible=false">Confirm</el-button>
        </div>
    </el-dialog>
</div>
</template>
<script>
export default {
    name:'branchDialog',
    componentName: 'branchDialog',
    data() {
        return {
            branchTableVisible:false,
            branchInfoList:[],
            subBranchList:[],
            defaultProps: {
                children: 'childrens',
                label: 'brhName',
                id:'brhId'
            },
            turnPageTotalNum:0,
            rules:{
                
            },
            tableData:[],
        }
    },
    methods:{
        handleClose(done) {  //机构选择
            done()
        },
        checkChange(data, checked) {
            let _this = this
            if (checked) {
                _this.$refs.tree.setCheckedKeys([data.brhId])
                this.$emit('getBranchForm',data)
                console.log("data"+JSON.stringify(data))
            }
        },
        getbranchInfoList() {
            let userInfo=this.$tools.storage.get("user","save")
            console.log("userInfo:"+JSON.stringify(userInfo))
            let userBranch=userInfo.subBranchList
            let _tableData = []
            let tableTemp=[];
            if(userInfo.brhLevel==1){//总行
                $.each(userBranch,function(i,m){
                    if(m.brhLevel==1){
                        m.childrens=[]
                        tableTemp.push(m);
                    }
                })
                
                $.each(tableTemp,function(i,m){
                    $.each(userBranch,function(j,n){
                        if(m.brhId==n.brhParentId){
                            m.childrens.push(n);
                        }
                    })
                })
                $.each(tableTemp,function(i,m){
                    $.each(m,function(j,n){
                        $.each(userBranch,function(k,p){
                            if(n.brhId==p.brhParentId){
                                n.childrens=[];
                                n.childrens.push(p)
                            }
                        })
                    })
                })
            }else if(userInfo.brhLevel==2){
                $.each(userBranch,function(i,m){
                    if(m.brhLevel==1){
                        m.childrens=[]
                        tableTemp.push(m);
                    }
                })
                $.each(tableTemp,function(i,m){
                    $.each(userBranch,function(j,n){
                        if(m.brhId==n.brhParentId){
                            m.childrens.push(n);
                        }
                    })
                })
            }else if(userInfo.brhLevel==3){
                $.each(userBranch,function(i,m){
                    if(m.brhLevel==1){
                        tableTemp.push(m);
                    }
                })
            }
            this.subBranchList=tableTemp;
        // this.$tools.request(this, "IM010103.do").then(data => {
            //     //console.log("data:"+JSON.stringify(data))
            //     if (data.body.errorCode == '0') {
            //         this.branchInfoList = data.body.subBranchList
            //         console.log("branchInfoList:"+JSON.stringify(this.branchInfoList))
            //         //总行
            //         if(this.userInfo.cstLevel==1){
            //             console.log("cstLevel:"+this.userInfo.cstLevel)
            //             for (let i = 0; i < this.branchInfoList.length; i++) {
            //                 if (this.branchInfoList[i].brhLevel == "1") {
            //                     _tableData.push(this.branchInfoList[i])
            //                 }
            //             }
            //             for (let j = 0; j < _tableData.length; j++) {
            //                 let brhId = _tableData[j].brhId
            //                 let childrens = []
            //                 for (let k = 0; k < this.branchInfoList.length; k++) {
            //                     if (brhId == this.branchInfoList[k].brhParentId) {
            //                         childrens.push(this.branchInfoList[k])
            //                         _tableData[j].childrens = childrens
            //                     }
            //                 }
            //             }
            //             for (let l = 0; l < _tableData.length; l++) {
            //                 for (let x = 0; x < _tableData[l].childrens.length; x++) {
            //                     let brhId1 = _tableData[l].childrens[x].brhId
            //                     let childrens1 = []
            //                     for (let y = 0; y < this.branchInfoList.length; y++) {
            //                         if (brhId1 == this.branchInfoList[y].brhParentId) {
            //                             childrens1.push(this.branchInfoList[y])
            //                             _tableData[l].childrens[x].childrens = childrens1
            //                         }
            //                     }
            //                 }
            //             }
            //             this.subBranchList = _tableData
            //             console.log("111:"+JSON.stringify(this.subBranchList))
            //             this.branchTableVisible=true;
            //         }
            //         //分行
            //         if(this.userInfo.cstLevel==2){
            //             console.log("cstLevel:"+this.userInfo.cstLevel)
            //             console.log("this.branchInfoList:"+JSON.stringify(this.branchInfoList))
            //             for(let i=0;i<this.branchInfoList.length;i++){
            //                 if (this.branchInfoList[i].brhLevel == "2") {
            //                     _tableData.push(this.branchInfoList[i])
            //                     console.log("222_1:"+JSON.stringify(_tableData))
            //                     break;
            //                 }
            //             }
            //             for (let j = 0; j < _tableData.length; j++) {
            //                 let brhId = _tableData[j].brhId
            //                 let childrens = []
            //                 for (let k = 0; k < this.branchInfoList.length; k++) {
            //                     if (brhId == this.branchInfoList[k].brhParentId) {
            //                         childrens.push(this.branchInfoList[k])
            //                         _tableData[j].childrens = childrens
            //                     }
            //                 }
            //             }
            //             this.subBranchList = _tableData
            //             console.log("222:"+JSON.stringify(this.subBranchList))
            //             this.branchTableVisible=true;
            //         }
            //         //支行
            //         if(this.userInfo.cstLevel==3){
            //             console.log("cstLevel:"+this.userInfo.cstLevel)
            //             //for(let i=0;i<this.branchInfoList.length;i++){
            //                 _tableData.push(this.branchInfoList[0])
            //                 this.subBranchList = _tableData
            //             //}
            //             this.branchTableVisible=true;
            //         }
            //     } else {
            //         this.$alert("System Error Try again", "warning");
            //     }
        // })
        },
        resetForm(queryForm) {
            this.$refs[queryForm].resetFields();
        },
        show(){
            this.branchTableVisible=true;
            this.getbranchInfoList()
            
        }
    },
    // mounted(){
    //     let _this = this
    //     _this.getbranchInfoList()
    // }

}
</script>
<style> 
.money .el-input{width:50%;}
.money .el-form-item{width:80%;}
.money .el-form-item__label{padding: 11px 12px 11px 133px;}
</style>